<?php
require_once __TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ . 'common.php';
// 获取当前用户名
$name = $user->__get('name');
$data = TeacherLogin_Plugin::getuser();
$option = TeacherLogin_Plugin::getoptions();
$group = $user->__get('group');

$wx = empty($data[$name]['wx']) ? '未绑定' : $data[$name]['wx'];
$qq = empty($data[$name]['qq']) ? '未绑定' : $data[$name]['qq'];
$wechat_work = empty($data[$name]['wechat_work']) ? '未绑定' : $data[$name]['wechat_work'];

if ($group != 'administrator' && !$option->users) { //非管理员且[非管理员启用]处于否
    throw new Typecho_Widget_Exception(_t('禁止访问'), 403);
}
?>
<!DOCTYPE HTML>
<html class="no-js">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TeacherLogin - 扫描登录授权绑定</title>
    <meta name="robots" content="noindex, nofollow">
    <link rel="stylesheet" href="<?= __TYPECHO_ADMIN_DIR__ ?>css/normalize.css?v=17.10.30">
    <link rel="stylesheet" href="<?= __TYPECHO_ADMIN_DIR__ ?>css/grid.css?v=17.10.30">
    <link rel="stylesheet" href="<?= __TYPECHO_ADMIN_DIR__ ?>css/style.css?v=17.10.30">
    <!--[if lt IE 9]>
    <script src="/admin/js/html5shiv.js?v=17.10.30"></script>
    <script src="/admin/js/respond.js?v=17.10.30"></script>
    <![endif]-->
</head>
<body class="body-100">
<!--[if lt IE 9]>
<div class="message error browsehappy" role="dialog">当前网页 <strong>不支持</strong> 你正在使用的浏览器. 为了正常的访问, 请 <a
        href="http://browsehappy.com/">升级你的浏览器</a>.
</div>
<![endif]-->
<div class="typecho-login-wrap">
    <div class="typecho-login" style="width: 100%;">
        <h1><a href="#" class="i-logo">Typecho</a></h1>
        <div class="qrlogin">
            <h3>用户授权：<?= $name ?></h3>
            <p>微信：<?= $wx ?> &nbsp;&nbsp;QQ：<?= $qq ?>&nbsp;&nbsp;企业微信：<?= $wechat_work ?></p>
            <div id="qrimg"></div>
            <div class="erweima" hidden></div>
            <p id='msg'>请扫码~</p>
            <hr/>
            <button type="submit" class="btn primary" onclick="reset()">重置绑定数据</button>
        </div>
        <p class="more-link"><a href="/admin/options-plugin.php?config=TeacherLogin">返回首页</a></p>
    </div>
</div>
<script src="<?= __TYPECHO_ADMIN_DIR__ ?>js/jquery.js?v=17.10.30"></script>
<script src="<?= __TYPECHO_ADMIN_DIR__ ?>js/jquery-ui.js?v=17.10.30"></script>
<script src="<?= __TYPECHO_ADMIN_DIR__ ?>js/typecho.js?v=17.10.30"></script>
<script src="https://ku.oioweb.cn/jquery.qrcode/jquery.qrcode.min.js"></script>
<script>
    var data = {};

    function bind(type, uin) {
        var api = "<?= TeacherLogin_Plugin::tourl('TeacherLogin/bind');?>";
        $.ajax({
            url: api,
            type: 'POST',
            data: {
                type: type,
                uin: uin
            },
            dataType: 'json',
            success: function (data) {
                alert(data.msg);
                window.location.reload();
            },
            error: function () {
                alert('绑定失败！~~');
            }
        });
    }

    function canvasToImage(canvas) {
        const image = new Image();
        image.src = canvas.toDataURL("image/png");
        return image;
    }

    function getqrocde() {
        var api = "<?= TeacherLogin_Plugin::tourl('TeacherLogin/getqrcode');?>";
        $.ajax({
            url: api,
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                window.data = data;
                if (data.code === 200) {
                    $('.erweima').qrcode({
                        render: "canvas",
                        width: 147,
                        height: 147,
                        foreground: "#1C1C1C",
                        background: "#FFFFFF",
                        correctLevel: 2,
                        text: data.qr_url
                    });
                    $('#qrimg').append(canvasToImage(document.getElementsByTagName('canvas')[0]));
                    if (window.id) {
                        window.clearInterval(window.id);
                    }
                    window.id = setInterval(getresult, 3000);
                } else {
                    alert('二维码获取失败！');
                }
            },
            error: function () {
                alert('二维码获取失败！');
            }
        });
    }

    function reset() {
        var api = "<?= TeacherLogin_Plugin::tourl('TeacherLogin/reset');?>";
        $.ajax({
            url: api,
            aycnc: false,
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                if (data.code === 200) {
                    alert(data.msg);
                }
                $("#msg").html(data.msg);
                window.location.reload();
            },
            error: function () {
                console.log('falil!~~');
            }
        });
    }

    function getresult() {
        var api = "<?= TeacherLogin_Plugin::tourl('TeacherLogin/getresult');?>";
        $.ajax({
            url: api,
            aycnc: false,
            type: 'POST',
            data: {token: data['token']},
            dataType: 'json',
            success: function (data) {
                let code = data.code;
                if (code === 1) {
                    $('#msg').html('请扫码~');
                }
                if (code === 2) {
                    $('#msg').html('扫码中~');
                }
                if (code === 3) {
                    bind(data.data.type, data.data.uin);
                    window.clearInterval(window.id);
                }
                if (code < 1 > 3) {
                    window.clearInterval(window.id);
                    $('#msg').html("二维码失效，请刷新页面");
                }
            },
            error: function () {
                console.log('登录结果获取失败！');
            }
        });
    }

    $(document).ready(function () {
        getqrocde();
    });

</script>
</body>
</html>
